package com.tapstitch.scm.common.utils;

import cn.hutool.core.date.LocalDateTimeUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.ISODateTimeFormat;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间工具类
 *
 * @author craft
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    public static String MM_DD_YYYY_HH_MM = "MMMM dd,yyyy\nhh:mm aa";

//    public static void main(String[] args) {
//        String yymmdd = dateTimeNow("YYMMdd");
//        System.out.println(yymmdd);
//    }

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }
    public static final String parseDateToStrDefault(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }


    public static boolean after(Date date1,Date date2) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        return c2.after(c1);
    }
    /**
     * 物流时间格式
    * */
    public static final String parseLogistics(String date) {
        date = date.replace("T", " ");
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
            Date time = sdf.parse(date);
            sdf = new SimpleDateFormat(MM_DD_YYYY_HH_MM, Locale.UK);
            return sdf.format(time).replace("AM", "am").replace("PM", "pm");
        } catch (ParseException e) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM);
                Date time = sdf.parse(date);
                sdf = new SimpleDateFormat(MM_DD_YYYY_HH_MM, Locale.UK);
                return sdf.format(time).replace("AM", "am").replace("PM", "pm");
            }catch (Exception e2) {
                return null;
            }
        }
    }

    public static final Date parseLogisticsDate(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(MM_DD_YYYY_HH_MM, Locale.UK);
            return sdf.parse(date);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 时间戳转date
     * @param date 10位时间戳转时间格式
     * @return
     */
    public static String getStringDate(Long date) {
        if (date == null) {
            return null;
        }
        Date dates = new Date(date*1000);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(dates);
    }

    public static String getStringDate2(Long date) {
        if (date == null) {
            return null;
        }
        Date dates = new Date(date*1000);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(dates);
    }

    public static String getStringDate(Long date, Locale locale) {
        if (date == null) {
            return null;
        }
        Date dates = new Date(date*1000);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale);
        return formatter.format(dates);
    }

    /**
     * description 获取当前时间戳
     * @return java.lang.Long
    **/
    public static Long getNow(){
        return System.currentTimeMillis() / 1000;
    }

    public static Long iso8601ToSecond(String iso8601) {
        if (StringUtils.isEmpty(iso8601)) {
            return null;
        }
        return ISODateTimeFormat.dateTimeNoMillis().parseMillis(iso8601) / 1000;
    }

    public static String secondToIso8601(Long seconds) {
        DateTime dateTime = new DateTime(seconds * 1000);
        return ISODateTimeFormat.dateTime().print(dateTime);
    }

    public static String secondToIso8601(Long seconds, DateTimeZone timeZone) {
        DateTime dateTime = new DateTime(seconds * 1000, timeZone);
        return ISODateTimeFormat.dateTime().print(dateTime);
    }

    /**
     * C# 日期型数据的长整型值是自 0001 年 1 月 1 日午夜 00:00,以来所经过时间以100 毫微秒为间隔表示时的数字
     * @return
     */
    public static long getTicks() {
        DateTime parse = DateTime.parse("0001-01-01 00:00:00", DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZone(DateTimeZone.UTC));
        return (System.currentTimeMillis() - parse.getMillis()) * 10000;
    }

    /**
     * 当前时间的偏移后的时间戳s
     * @param offset
     * @return
     */
    public static Long getBeginDayByOffset(int offset){
        LocalDateTime localDateTime = LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.offset(LocalDateTime.now(),offset, ChronoUnit.DAYS));
        return localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
    }


}
